package com.zq.it.service.impl.admin;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.zq.it.pojo.Academy;
import com.zq.it.service.admin.AcademyService;
import com.zq.it.service.common.BaseServiceImpl;
import com.zq.it.tool.HeadTitle;

public class AcademyServiceImpl extends BaseServiceImpl implements AcademyService {

	//private List<Academy> ; //存储查询数据
	@Override
	public List<Academy> getAllAcademy() {		
		// TODO Auto-generated method stub
		return getAcademyDao().findAcademyEtyList();
	}

	@Override
	public void removeAcademy(Integer id) {
		getAcademyDao().deleteAcademyEtyByID(id);
		// TODO Auto-generated method stub
	
	}

	@Override
	public void renewalAcademy(Academy academy) {
        getAcademyDao().updateAcademyEty(academy);
	}

	@Override
	public List getAcademy(Integer id) {
		return getAcademyDao().findAcademyEtyById(id);
	}

	@Override
	public void addAcademy(Academy academy){	
		getAcademyDao().saveAcademyEty(academy);
		// TODO Auto-generated method stub
		
	}

	@SuppressWarnings("deprecation")
	@Override
	public List getAcademyListByType(String type,String parameter) {
		List list=null;
		if(type.equals("aId"))
			list= getAcademyDao().findAcademyEtyById(Integer.parseInt(parameter));
		if(type.equals("aNa"))
			list=getAcademyDao().findAcademyEtyByName(parameter);
		if(type.equals("aDa"))
			list=getAcademyDao().findAcademyEtyByDate(com.zq.it.tool.Constant.getDateFromStr(parameter));			
		return list;
	}

	@Override
	public List getAcademyByExmp(Academy academy) {
		// TODO Auto-generated method stub
		return getAcademyDao().findAcademyEtyByExmp(academy);
	}

	@Override
	public void removeAcademy(Academy academy){
		// TODO Auto-generated method stub
		getAcademyDao().deleteAcademyEtyByObj(academy);
	}

	@Override
	public void removeAllAcademy(int[] ids,List<Academy> list) {
		getAcademyDao().deleteAllEty(getListFromTemplate(ids,list));
		// TODO Auto-generated method stub
		
	}
	
	private List getListFromTemplate(int [] ids,List<Academy> alist){
		List<Academy> list=new ArrayList<Academy>();
	  for(Academy t:alist){
		 for(int i=0;i<ids.length;i++)
			 if(t.getAcademyId().equals(ids[i]))
		        list.add(t);
	  }
	  return list;
	}

	@Override
	public List<Academy> importAcademyList(File acmyFile){
		  List<Academy> acmyList=getAcmyListFromStr(importxlsxFile(acmyFile));
		  List<Academy> existAcmyList=new ArrayList<Academy>();
		  
		 for(int i=0;i<acmyList.size();i++){		
			 Academy acmy=acmyList.get(i);
			 if(!getAcademyDao().findAcademyEtyById(acmy.getAcademyId()).isEmpty())
				 existAcmyList.add(acmy);
			//System.out.println(acmy.getCreateTime());
		 }
		     acmyList.removeAll(existAcmyList);
		   if(!acmyList.isEmpty())
		    	getAcademyDao().saveAcademyEtyList(acmyList);
			return existAcmyList;
		}	
		
		
		
		 private  List<Academy> getAcmyListFromStr(String str){		
			List<Academy> list=new ArrayList<Academy>();
			String [] firstArray=str.split(";");
			for(int i=0;i<firstArray.length;i++){
				String [] secondArray=firstArray[i].split(":");
				Academy academy=new Academy();
				academy.setAcademyId(Integer.parseInt(secondArray[0]));
				academy.setAcademyName(secondArray[1]);
				academy.setAcademyIntro(secondArray[2]);	
				academy.setCreateTime(com.zq.it.tool.Constant.getDateFromStr(secondArray[3]));
				list.add(academy);
			}
			
			return list;
		}
		 
	//数据的导出
	@Override
		public ByteArrayInputStream exportAcademyList(String[] headTitle,List<?> list) {
			// TODO Auto-generated method stub
			return new ByteArrayInputStream(exportToExcel(list,headTitle,HeadTitle.EN_ACMYHEAD.getArray(),"com.zq.it.pojo.Academy").toByteArray());
		}
		
}
